﻿import QtQuick 2.7
import QtQuick.Controls 1.5
import QtGraphicalEffects 1.0

//import com.leamus.gamedata 1.0

import "../global.js" as GlobalConst
import "../Poke/Temp.js" as PokeClass

Item {
    id: root

    property int flags
    property var userData

    property bool bDead
    property bool bEnable : (flags & PokeClass.Flag_Enable)     //是否可以被选择
    property bool bClicked : (flags & PokeClass.Flag_Clicked)          //点击状态

    property alias source: imagePlayer.source
    property alias sourceSize: imagePlayer.sourceSize


    property int shouPaiCount
    property int bloodCount: 0
    property int bloodMaxCount: 0

    property var bloodImageArray: new Array //缓存血量的图片
    property var bloodArray: new Array //血量数组(5个)

    signal clicked(bool bClicked, var userData);            //点击发送信号,参数为状态

    //visible: flags & PokeClass.flag_Show


    //单个血量图片
    Component {
        id: compBloodArray
        Image {
            //x: 105
            y: 63
            width: 18
            height: 22
        }
    }

    //血量图片缓存
    Component {
        id: componentImage
        Image {
            anchors.fill: parent
            visible: false
        }
    }

    //这里是边框效果
    RectangularGlow {
        id: rectGlow
        visible: root.bEnable && (root.bClicked)// || mouseArea.containsMouse)
        anchors.fill: playerBack;
        glowRadius: 7  //边框宽度
        spread: 0.4;    //虚化程度(0~1)
        color: root.bClicked ? "lightgreen" : "blue"  //颜色
        //cornerRadius: rect.radius + glowRadius
    }

    //武将背景
    Image {
        id: playerBack
        source: "photo-back.png"

        //武将图片
        Image {
            id: imagePlayer
            x: 6
            y: 26
        }

        //颜色遮罩(不可用时黑色)
        ColorOverlay {
            id: colorOverlay
            visible: !root.bEnable
            anchors.fill: imagePlayer;
            source: imagePlayer;
            color: "#80000000"
        }

        MouseArea {
            id: mouseArea
            enabled: root.bEnable
            hoverEnabled: true
            anchors.fill: parent
            //hoverEnabled: true
            onPressed: {
                if(bEnable)
                {
                    //bClicked = !bClicked
                    //flags ^= PokeClass.flag_Clicked

                    /*
                    console.debug(GameCore.playerList[userData].flag)
                    console.debug(GameCore.playerList[userData])
                    console.debug(GameCore.playerList[userData].id)
                    console.debug(bEnable,bClicked,userData)
                    */
                    root.clicked(bClicked,userData);
                }
            }
            //onEntered: console.debug(rectGlow.visible)
        }


    }
    Text {
        id: paiDuiCount
        x: 10
        y: 10
        width: 20
        height: 10
        z: 100
        text: qsTr("0")

        font.pixelSize: 12;
        style: Text.Outline;
        styleColor: "lightgreen"
    }

    //设置血量
    function setBlood(blood,maxBlood) {
        if(maxBlood !== undefined && maxBlood >= 0)
            bloodMaxCount = maxBlood;
        if(blood !== undefined && blood >= 0)
            bloodCount = blood;

        refreshBloodShow()
    }
/*
    function setPlayer(src) {
        imagePlayer.source = src
    }
*/


    //刷新显示
    function refreshBloodShow() {
        for(var i = 0; i < 5; i++) {
            if(i < bloodMaxCount) {         //是否显示此血槽
                bloodArray[i].visible = true;
                if(bloodCount > 5 && (i < bloodCount % 5 || bloodCount / 5 >= 2 ))  //超过5血,显示绿色
                    bloodArray[i].source = bloodImageArray[2].source;
                else if(i < bloodCount)     //5血以下,红色
                    bloodArray[i].source = bloodImageArray[1].source;
                else        //空位为黑色
                    bloodArray[i].source = bloodImageArray[0].source;
            }
            else
                bloodArray[i].visible = false;
        }
    }

    onBloodCountChanged: refreshBloodShow()
    onBloodMaxCountChanged: refreshBloodShow()

    onShouPaiCountChanged: {
        paiDuiCount.text = shouPaiCount
    }

    Component.onCompleted: {
        //for(var i = 0; i < 2; i++) {
        //imageArray[i] = Qt.createQmlObject('import QtQuick 2.2; Image {anchors.fill: parent}',root);
        //    bloodImageArray[i] = componentImage.createObject(root,{"source": "Blood/0.png"})
        //}
        bloodImageArray[0] = componentImage.createObject(root,{"source": "Blood/0.png"})
        bloodImageArray[1] = componentImage.createObject(root,{"source": "Blood/1.png"})
        bloodImageArray[2] = componentImage.createObject(root,{"source": "Blood/5.png"})
        //imageArray[0].visible = true

        for(var i = 0; i < 5; i++) {
            bloodArray[i] = compBloodArray.createObject(playerBack,{"x": 105-i*18})
        }
        //setPlayer("erzhang.png")
        //setBlood(10,10)
        //root.enabled = false;
    }
}


